//
//  FavoriteViewController.m
//  ksjv2
//
//  Created by mac on 14/12/23.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//

#import "FavoriteViewController.h"
#import "Util.h"
#import "ArticleViewController.h"
@interface FavoriteViewController (){
    NSMutableArray*array;
}

@end

@implementation FavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"收藏";
    array=[[NSMutableArray alloc]init];
    [SVProgressHUD show];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    [self loadNews];
    
    
    UIButton *buttonf=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonf setFrame:CGRectMake(0, 0, 22, 22)];
    [buttonf addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [buttonf setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
        //            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [buttonf setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        
    }
       UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:buttonf];
    self.navigationItem.leftBarButtonItem=leftBarItem;
    // Do any additional setup after loading the view.
}
-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)loadNews{
    
    [[AppAPIClient sharedClient] GET:@"Favorite/List" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary*dic in responseObject[@"data"]) {
            [array addObject:dic];
        }
        
        if (array.count==0) {
            
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:self.view.frame];
            [button setImage:[UIImage imageNamed:@"article_logo"] forState:UIControlStateNormal];
            [button setEnabled:NO];
            [self.view addSubview:button];
            
        }
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERR: %@",operation.responseString);

    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    
    if (cell==nil) {
        
        cell=[tableView dequeueReusableCellWithIdentifier:@"cell1"];
    }
    UILabel*label1=(UILabel*)[cell.contentView viewWithTag:11];
    label1.text=[NSString stringWithFormat:@"%@",array[indexPath.row][@"title"]];
    
    UILabel*label2=(UILabel*)[cell.contentView viewWithTag:22];
    NSDate * dt = [NSDate dateWithTimeIntervalSince1970:[array[indexPath.row][@"adddate"] floatValue]];
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd mm:ss"];
    NSString *regStr = [df stringFromDate:dt];
    label2.text=[NSString stringWithFormat:@"%@",regStr];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ArticleViewController*art=[story instantiateViewControllerWithIdentifier:@"ArticleViewController"];
    NSDictionary*_dic=array[indexPath.row];
    NSMutableDictionary*dic=[NSMutableDictionary dictionaryWithDictionary:_dic];
    NSString*url=_dic[@"url"];
    if (url.length<5) {
        url=_dic[@"content"];
    }
    NSArray*ksjv2=[url componentsSeparatedByString:@"ksjv2.kanshangjie.com"];
    if (ksjv2.count<2) {
        return;
    }
    NSArray *arr = [url componentsSeparatedByString:@"?"];
    NSArray*arr2=[arr[1] componentsSeparatedByString:@"&"];
    for (NSString*str in arr2) {
        NSArray*strA=[str componentsSeparatedByString:@"="];
        if ([strA[0] isEqualToString:@"catid"]) {
            [dic setObject:strA[1] forKey:@"catid"];
            break;
        }
    }
    art._Dict=dic;
    art.isCommentVC=NO;
    art.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:art animated:YES completion:^{[art theFavoriteYes];}];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //        获取选中删除行索引值
        
        
        [[AppAPIClient sharedClient] GET:@"Favorite/Remove" parameters:@{@"fid": array[row][@"id"]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"ERR: %@",operation.responseString);
            
        }];
        //        通过获取的索引值删除数组中的值
        [array removeObjectAtIndex:row];
        //        删除单元格的某一行时，在用动画效果实现删除过程
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
